# server.py
# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
from cgi import parse_qs, escape




def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    a = ''
    # for key in environ:
    #     print(key + "======")
    # body = '<h1>Hello, %s!</h1>' % (environ['QUERY_STRING'] or 'web')
    # return [body.encode('utf-8')]

    # arg = environ['QUERY_STRING']
    path = environ['PATH_INFO']
    if path == "/submit":
        print('1111111')
        body = '<form action="http://localhost:8000/seuus" method="post"  > <input type="text" name="ttt" value="cccccc"> <input type="submit" value="submit" ></form>'
        return [body.encode('utf-8')]
    elif path == "/seuus":
        body = "seccus"
        v = environ['QUERY_STRING']
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
        request_body = environ['wsgi.input'].read(request_body_size)
        d = parse_qs(request_body)
        # print(d.dir())
        ttt = d.get('ttt', [''])[0] # 返回第一个age的值
        return [ttt.encode('utf-8')]

    print('333333')
    return ['ZZZZZZZZZCCCCC'.encode('utf-8')]

httpd = make_server('', 8000, application)
print('Serving HTTP on port 8000...')
# 开始监听HTTP请求:
httpd.serve_forever()
